package zjut.raymond.collectorplus.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.lifecycle.ViewModelProvider;

import java.util.Objects;
import java.util.regex.Pattern;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;
import zjut.raymond.collectorplus.R;
import zjut.raymond.collectorplus.util.ApiResult;
import zjut.raymond.collectorplus.util.Constant;
import zjut.raymond.collectorplus.vm.MainViewModel;

public class AddRPDialog extends DialogFragment implements View.OnClickListener {
    private MainViewModel viewModel;
    private EditText editTextCoordX, editTextCoordY;
    private Button btnCancel, buttonOK;
  //  private Handler handler;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);
        return inflater.inflate(R.layout.dialog_add_one, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewModel = new ViewModelProvider(getActivity()).get(MainViewModel.class);
        editTextCoordX = getView().findViewById(R.id.editTextRpCoordx);
        editTextCoordY = getView().findViewById(R.id.editTextRpCoordY);
        btnCancel = getView().findViewById(R.id.btn_cancel);
        buttonOK = getView().findViewById(R.id.btn_ok);
        btnCancel.setOnClickListener(this);
        buttonOK.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel: {
                getDialog().dismiss();
            }
            break;
            case R.id.btn_ok: {
                String stringX = editTextCoordX.getText().toString();
                String stringY = editTextCoordY.getText().toString();
                int x = (isNumeric(stringX)) ? Integer.parseInt(stringX) : 0;
                int y = (isNumeric(stringY)) ? Integer.parseInt(stringY) : 0;
                String coord = x + "," + y;
                RequestParams requestParams = new RequestParams();
                requestParams.addFormDataPart("amount", 1);
                requestParams.addFormDataPart("business_id", Objects.requireNonNull(viewModel.getCurrentBusiness().getValue()).getId());
                requestParams.addFormDataPart("area_id", Objects.requireNonNull(viewModel.getCurrentBuilding().getValue()).getId());
                requestParams.addFormDataPart("floor_id", Objects.requireNonNull(viewModel.getCurrentFloor().getValue()).getId());
                requestParams.addFormDataPart("coord", coord);
                new Thread(() -> HttpRequest.put(Constant.ADD_REFERENCE_POINT_URL, requestParams, new BaseHttpRequestCallback<ApiResult>() {
                    @Override
                    protected void onSuccess(ApiResult apiResult) {
                        if (apiResult.getCode() == 0) {
                            Toast.makeText(getContext(), "添加成功", Toast.LENGTH_SHORT).show();
                            getDialog().dismiss();
                            MainFragment.handler.sendEmptyMessage(MainFragment.MSG_REQUEST);
                        } else {
                            Toast.makeText(getContext(), "添加失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(int errorCode, String msg) {
                        super.onFailure(errorCode, msg);
                        Toast.makeText(getContext(), "添加失败", Toast.LENGTH_SHORT).show();
                    }
                })).start();
                break;
            }
            default:
                break;
        }

    }

    public boolean isNumeric(String str) {
        return Pattern.matches("[0-9]+", str);
    }

}
